unit UDF;

interface

uses
  Vcl.Forms, System.SysUtils, uADCompClient, uADPhysIB, System.IniFiles,
  IdHashMessageDigest, IdGlobal, IdHash, Vcl.Dialogs, uADPhysManager;

procedure SetDBInfo(var ADConnection: TADConnection; var ADPhysDriverLink: TADPhysDriverLink);
procedure DoQuerySQL(var ADConnection: TADConnection; var ADQuery: TADQuery);
function MD5(aStr: string): string;
function DBDriverID: string;
function ConfigPath: string;
function DBPath: string;
procedure SaveDefaultConfigIniFile;

type
  TDBOp = (opInsert, opUpdate, opDenied);

var
  VariantManager: TVariantManager;

implementation

function DBPath: string;
begin
  Result := ExtractFilePath(Application.ExeName) + 'DB\MYLONGLONG.FDB';
end;

function ConfigPath: string;
begin
  Result := ExtractFilePath(Application.ExeName) + 'Config.ini';
end;

procedure SaveDefaultConfigIniFile;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ConfigPath);
  try
    //MySQL
    Ini.WriteString('Config', 'DriverID', 'MySQL');
    Ini.WriteString('MySQL', 'Database', 'mylonglong');
    Ini.WriteString('MySQL', 'Username', 'root');
    Ini.WriteString('MySQL', 'Password', '1234');
    Ini.WriteString('MySQL', 'Protocol', 'Remote');
    Ini.WriteString('MySQL', 'Server', 'localhost');
    Ini.WriteString('MySQL', 'VendorHome', 'C:\Program Files\Firebird\Firebird_2_5');
    Ini.WriteString('MySQL', 'VenderLib', 'fbclient.dll');
    //Firebird
    Ini.WriteString('Firebird', 'Database', DBPath);
    Ini.WriteString('Firebird', 'Username', 'sysdba');
    Ini.WriteString('Firebird', 'Password', 'masterkey');
    Ini.WriteString('Firebird', 'Protocol', 'Local');
    Ini.WriteString('Firebird', 'Server', '');
    Ini.WriteString('Firebird', 'VendorHome', 'C:\Program Files\Firebird\Firebird_2_5');
    Ini.WriteString('Firebird', 'VenderLib', 'fbclient.dll');
  finally
    Ini.Free;
  end;
end;

procedure SetDBInfo(var ADConnection: TADConnection; var ADPhysDriverLink: TADPhysDriverLink);
var
  Server: string;
  DriverID: string;
  Password: string;
  Ini: TIniFile;
  VendorLib: string;
  Database: string;
  Protocol: string;
  VendorHome: string;
  Username: string;
begin
  if not FileExists(ConfigPath) then
    SaveDefaultConfigIniFile;
  Ini := TIniFile.Create(ConfigPath);
  try
    //default DB setting is for Firebird
    DriverID := DBDriverID;
    Database := Ini.ReadString(DriverID, 'Database', DBPath);
    Username := Ini.ReadString(DriverID, 'Username', 'sysdba');
    Password := Ini.ReadString(DriverID, 'Password', 'masterkey');
    Protocol := Ini.ReadString(DriverID, 'Protocol', 'Local');
    Server := Ini.ReadString(DriverID, 'Server', '');
    VendorHome := Ini.ReadString(DriverID, 'VendorHome', 'C:\Program Files\Firebird\Firebird_2_5');
    VendorLib := Ini.ReadString(DriverID, 'VenderLib', 'fbclient.dll');
  finally
    Ini.Free;
  end;
  ADPhysDriverLink.DriverID := DriverID;
  ADPhysDriverLink.VendorHome := VendorHome;
  ADPhysDriverLink.VendorLib := VendorLib;
  ADConnection.DriverName := DriverID;
  ADConnection.Params.Values['DriverID'] := DriverID;
  ADConnection.Params.Values['Database'] := Database;
  ADConnection.Params.Values['User_Name'] := Username;
  ADConnection.Params.Values['Password'] := Password;
  ADConnection.Params.Values['Protocol'] := Protocol;
  ADConnection.Params.Values['Server'] := Server;
  ADConnection.Connected := True;
end;

function MD5(aStr: string): string;
var
  MyMD5: TIdHashMessageDigest5;
begin
  MyMD5 := TIdHashMessageDigest5.Create;
  MD5 := MyMD5.HashStringAsHex(aStr);
  MyMD5.Free;
end;

procedure DoQuerySQL(var ADConnection: TADConnection; var ADQuery: TADQuery);
begin
  with ADQuery do
  begin
    try
      ADConnection.StartTransaction;
      if not Prepared then
        Prepare;
      ExecSQL;
      ADConnection.Commit;
    except
      on E: Exception do
      begin
        ShowMessage(Format('%s', [E.Message]));
        ADConnection.Rollback;
      end;
    end;
  end;
end;

function DBDriverID: string;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(ConfigPath);
  try
    //default DB setting is for Firebird
    Result := Ini.ReadString('Config', 'DriverID', 'Firebird');
  finally
    Ini.Free;
  end;
end;

end.
